<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.global.js"></script>
  </head>
  <body>
    <div id="app">
      <son></son>
    </div>

    <script>
      const i18nPlugin = {
        install(app, option) {
          app.provide("suchabiao", option);

          // greetings.WhatAStupid
          app.config.globalProperties.$fanyi = (key) => {
            return key.split(".").reduce((a, b) => a[b], option);
          };

          let elem = null
          let targetColor
          const highlightHandler = () => {
            elem.style.color = targetColor;
          };
          const highlightRemover = () => {
            elem.style.color = "black";
          };

          app.directive("highlight", {
            mounted(el, binding) {
              elem = el
              console.log("binding.value",binding.value);
              targetColor = binding.value || 'red'
              el.addEventListener("mouseover", highlightHandler);
              el.addEventListener("mouseout", highlightRemover);
            },
            beforeUnmount(el, binding) {
              elem = el
              el.removeEventListener("mouseover", highlightHandler);
              el.removeEventListener("mouseout", highlightRemover);
            },
          });
          
        },
      };

      const app = Vue.createApp({});

      app.use(i18nPlugin, {
        greetings: {
          hi: "嗨!",
          WhatAStupid: "真是个小可爱!",
        },
      });

      app.component("son", {
        inject: ["suchabiao"],
        created() {
          console.log("son created", this.suchabiao);
        },
        template: `
        <span v-highlight="'blue'" >son: {{$fanyi('greetings.WhatAStupid')}}</span>
        `,
      });
      app.mount("#app");
    </script>
  </body>
</html>
